package client

import (
	"fmt"
	"net"
)

// Client 客户端程序
func Client() {

	conn, err := net.Dial("tcp", "127.0.0.1:8000")
	if err != nil {
		fmt.Println("net.Dial err = ", err)
		return
	}
	defer conn.Close()

	//客户端读信息协程
	go func() {
		var buf [1024]byte
		var n int
		for {
			n, err = conn.Read(buf[0:])
			if err != nil {
				fmt.Println("read from conn failed, err:", err)
				break
			}
			fmt.Println(string(buf[:n]))
		}
	}()

	//客户端发送信息
	// inputReader := bufio.NewReader(os.Stdin)
	for {
		// input, err := inputReader.ReadString('\n')
		// if err != nil {
		// 	fmt.Println("read from console failed, err:", err)
		// 	break
		// }
		// trimmedInput := strings.TrimSpace(input)

		var input string
		fmt.Scan(&input)
		input += "\n"
		if input == "bye" {
			break
		}
		_, err1 := conn.Write([]byte(input))
		if err1 != nil {
			fmt.Printf("write failed , err : %v\n", err)
			break
		}

	}

}
